package com.bjzsly.star3.shareclick.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bjzsly.star3.xzb.auth.TxShareUtils;
import com.bjzsly.star3.xzb.utils.DeviceUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by ly on 2017/10/19.
 * do share wx by useing other app signature
 */

public class WeChatUtils {

    private static boolean isOwnApp = true;

//    private static String WX_APPID = "wxa5d77cd210b4d02b";
//    private static String WX_APPID = "wx30bd09b7b0561fbe";
    private static String WX_APPID = "wxa5d77cd210b4d02b";

    private static String curPkg;


    public static final int TYPE_SHARE_WX_CIRCLE = 0X01;
    public static final int TYPE_SHARE_WX_FRIEDN = 0x02;
    public static final int TYPE_WX_AUTH_LOGIN = 0x03;


    public static void configType(Context ctx, int type) {
        if (ctx == null) return;
        switch (type) {
            case TYPE_SHARE_WX_FRIEDN:
            case TYPE_SHARE_WX_CIRCLE:
                String info[] = getUserfulPkg(ctx);
                String pkgName = info[1];
                String appId = info[0];
                if (!TextUtils.isEmpty(pkgName) && !TextUtils.isEmpty(appId)) {
                    TxShareUtils.getInstance().recreateWxFactory(ctx, appId);
                }
                curPkg = pkgName;
                isOwnApp = false;
                break;

            case TYPE_WX_AUTH_LOGIN:
                TxShareUtils.getInstance().recreateWxFactory(ctx, WX_APPID);
                curPkg = ctx.getPackageName();
                Log.d("Xiang",curPkg);
                isOwnApp = true;
                break;
        }
    }


    public static String getPackageName(Context ctx) {

        try {
            if (isOwnApp) {
//                TxShareUtils.getInstance().recreateWxFactory(ctx, WX_APPID);
                return ctx.getPackageName();
            }

            if (!TextUtils.isEmpty(curPkg)) return curPkg;

            String info[] = getUserfulPkg(ctx);
            String pkgName = info[1];
            String appId = info[0];
            if (!TextUtils.isEmpty(pkgName) && !TextUtils.isEmpty(appId)) {
//                TxShareUtils.getInstance().recreateWxFactory(ctx, appId);
                return pkgName;
            }

        } catch (Exception e) {
//            TxShareUtils.getInstance().recreateWxFactory(ctx, WX_APPID);
            return ctx.getPackageName();
        }

        TxShareUtils.getInstance().recreateWxFactory(ctx, WX_APPID);
        return ctx.getPackageName();
    }


    private static String[] getUserfulPkg(Context ctx) {

        try {
            InputStream is = ctx.getAssets().open("p");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String result = sb.toString().trim();
            String infos[] = result.split("[|]");
            for (int i = 0; i < infos.length; i++) {
                String pkgs[] = infos[i].split(",");
                String pkg = pkgs[1];
                if (DeviceUtil.isAppInstall(ctx, pkg)) {
                    return pkgs;
                }
            }

        } catch (Exception e) {
        }

        return new String[]{ctx.getPackageName(), WX_APPID};
    }


}

